package com.initial.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.entity.TbCrmLeads;
import com.initial.entity.TbSystemUser;
import com.initial.mapper.TbCrmLeadsMapper;
import com.initial.service.TbCrmLeadsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LeadController {
    @Resource
    private TbCrmLeadsMapper tbCrmLeadsMapper;
    @Resource
    private TbCrmLeadsService tbCrmLeadsService;

    @RequestMapping("/toLead")
    public String toLead() {
        return "views/manage/lead/list";
    }

    //    获取分页数据
    @RequestMapping("/listLead")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows, String name){
        Map<String,Object> map = new HashMap<>();
        List<TbCrmLeads> tbCrmLeads = tbCrmLeadsMapper.pageSearch(new Page<TbCrmLeads>(page,rows));
//        获取到期天数,超出则显示过期
        tbCrmLeads.forEach(tbCrmLeads1 -> {
            if (Integer.parseInt(tbCrmLeads1.getDffday().toString()) < 0) {
                tbCrmLeads1.setDffday("已过期");
            }else if (Integer.parseInt(tbCrmLeads1.getDffday().toString()) == 0) {
                tbCrmLeads1.setDffday("请尽快于今天进行");
            }
        });
//         获取数据总数
        Integer integer = tbCrmLeadsMapper.countInteger();
//        前台显示数据总条数
        map.put("total",integer);
//        每一页显示的数据量
        map.put("rows",tbCrmLeads);
        System.out.println(map);
        return map;
    }

//    查看线索信息
    @RequestMapping("/toSee")
    public String toSee(Integer id,  Model model){
        TbCrmLeads tbCrmLeads = tbCrmLeadsMapper.see(id);
        model.addAttribute("leads",tbCrmLeads);
        return "views/manage/lead/show_leads";
    }

//    去修改页面
    @RequestMapping("/toEditLead")
    public String toEdit(Integer id,Model model){
        TbCrmLeads tbCrmLeads = tbCrmLeadsMapper.see(id);
        model.addAttribute("leads",tbCrmLeads);
        List<TbSystemUser> ids = tbCrmLeadsMapper.ids();
        model.addAttribute("users",ids);
        return "views/manage/lead/edit";
    }

//    修改页面
    @RequestMapping("/EditLead")
    @ResponseBody
    public ModelMap editLead(@RequestBody TbCrmLeads tbCrmLeads){
        ModelMap map = new ModelMap();
        boolean update = tbCrmLeadsService.updateById(tbCrmLeads);
        if(update){
            map.addAttribute("d",true);
            map.addAttribute("msg","修改成功！");
        }else{
            map.addAttribute("d",false);
            map.addAttribute("msg","修改失败！");
        }
        return map;
    }

//    去添加页面
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<TbSystemUser> ids = tbCrmLeadsMapper.ids();
        model.addAttribute("users",ids);
        return "views/manage/lead/add";
    }

//    添加页面
    @RequestMapping("/Add")
    @ResponseBody
    public ModelMap add(@RequestBody TbCrmLeads tbCrmLeads, HttpSession session){
        ModelMap map = new ModelMap();
        TbSystemUser user = (TbSystemUser)session.getAttribute("user");
        tbCrmLeads.setCreatorUserId(user.getId());
        boolean save = tbCrmLeadsService.save(tbCrmLeads);
        if(save){
            map.addAttribute("d",true);
            map.addAttribute("msg","添加成功！");
        }else{
            map.addAttribute("d",false);
            map.addAttribute("msg","添加失败！");
        }
        return map;
    }

//    删除功能,可以批量删除
    @RequestMapping("/toDeleteLeads")
    @ResponseBody
    public ModelMap deleteLeads(String[] ids){
        ModelMap map = new ModelMap();
        boolean judge = tbCrmLeadsService.removeByIds(Arrays.asList(ids));
        if (judge){
            map.put("success",true);
            map.put("message","删除成功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }
}
